package com.jiangyg.mall.core.utils.tree;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.*;

/**
 * 类描述：树形结构节点信息
 *
 * @author jiangyg
 * @version 2.0
 * @date 2019-10-14
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Tree implements Serializable {

    private static final long serialVersionUID = -11136715842530084L;

    /**
     * 节点ID
     */
    private String id;

    /**
     * 节点名称
     */
    private String name;

    /**
     * 父节点ID
     */
    private String pid;

    /**
     * 排序
     */
    private Integer sort;

    /**
     * 子集节点集合
     */
    private List<Tree> children;

    /**
     * 附加属性
     */
    private Map<String, Object> attributes;

    /**
     * 功能描述：追加子节点
     *
     * @param ts 子节点数组
     */
    public void addChildren(Tree... ts) {
        if (ts == null || ts.length == 0) {
            return;
        }
        if (children == null) {
            children = new ArrayList<>();
        }
        Collections.addAll(children, ts);
    }

    /**
     * 功能描述：追加属性
     *
     * @param key   key
     * @param value value
     */
    public void addAttribute(String key, Object value) {
        if (attributes == null) {
            attributes = new HashMap<>();
        }
        attributes.put(key, value);
    }

}
